<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue'
import { useRouter } from 'vue-router'
import { getCurrentUser, logout } from '../services/auth'

const router = useRouter()
const isScrolled = ref(false)
const showDropdown = ref(false)
const currentUser = ref(getCurrentUser())

// 刷新用户信息
const refreshUser = () => {
  currentUser.value = getCurrentUser()
}

// 登出处理
const handleLogout = () => {
  logout()
  currentUser.value = null
  router.push('/')
}

// 暴露给父组件的方法和状态
defineExpose({
  refreshUser,
  currentUser
})

// 监听滚动事件，用于导航栏样式变化
const handleScroll = () => {
  isScrolled.value = window.scrollY > 10
}

// 导航到个人中心
const goToProfile = () => {
  router.push('/user/profile')
}

// 导航到首页
const goToHome = () => {
  router.push('/')
}

// 关闭下拉菜单的函数
const closeDropdownOnClickOutside = (event) => {
  if (showDropdown.value && !event.target.closest('.user-avatar-container')) {
    showDropdown.value = false
  }
}

onMounted(() => {
  window.addEventListener('scroll', handleScroll)
  document.addEventListener('click', closeDropdownOnClickOutside)
})

onBeforeUnmount(() => {
  window.removeEventListener('scroll', handleScroll)
  document.removeEventListener('click', closeDropdownOnClickOutside)
})
</script>

<template>
  <header class="navbar" :class="{ 'navbar-scrolled': isScrolled }">
    <div class="navbar-container">
      <!-- 左侧LOGO -->
      <div class="navbar-left">
        <button class="mobile-menu-btn" @click="$emit('toggle-mobile-menu')">
          <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
            <line x1="3" y1="12" x2="21" y2="12"></line>
            <line x1="3" y1="6" x2="21" y2="6"></line>
            <line x1="3" y1="18" x2="21" y2="18"></line>
          </svg>
        </button>
        <div class="logo" @click="goToHome">
          <svg class="logo-icon" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
            <circle cx="50" cy="50" r="45" fill="#3a86ff"/>
            <path d="M30 35 L70 35 L70 65 L30 65 Z" fill="white"/>
            <path d="M40 45 L60 45 L60 55 L40 55 Z" fill="#3a86ff"/>
            <path d="M70 40 L80 45 L70 50 Z" fill="white"/>
          </svg>
          <span class="logo-text">电影天地</span>
        </div>
      </div>

      <!-- 中间导航链接（桌面端） -->
      <nav class="nav-links desktop-only">
        <a href="/" class="nav-link" @click.prevent="goToHome">首页</a>
        <a href="/ai-recommendations" class="nav-link ai-nav-link" @click.prevent="router.push('/ai-recommendations')">AI推荐</a>
        <a href="/category/action" class="nav-link" @click.prevent="router.push('/category/action')">动作</a>
        <a href="/category/comedy" class="nav-link" @click.prevent="router.push('/category/comedy')">喜剧</a>
        <a href="/category/drama" class="nav-link" @click.prevent="router.push('/category/drama')">剧情</a>
        <a href="/category/scifi" class="nav-link" @click.prevent="router.push('/category/scifi')">科幻</a>
        <a href="/membership" class="nav-link" @click.prevent="router.push('/membership')">会员中心</a>
        <a href="/customer-service" class="nav-link" @click.prevent="router.push('/customer-service')">客服</a>
      </nav>

      <!-- 右侧用户区域 -->
      <div class="navbar-right">
        <!-- 搜索按钮 -->
        <button class="nav-button" @click="router.push('/search')">
          <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
            <circle cx="11" cy="11" r="8"></circle>
            <line x1="21" y1="21" x2="16.65" y2="16.65"></line>
          </svg>
        </button>
        
        <!-- 用户头像或登录注册按钮 -->
        <div v-if="currentUser" class="user-menu">
          <div class="user-avatar-container">
            <button class="user-avatar" @click.stop="showDropdown = !showDropdown">
              <img :src="currentUser.avatar" :alt="currentUser.username" />
            </button>
            <div class="user-dropdown" v-if="showDropdown" @click.stop>
                <button class="dropdown-item" @click="goToProfile(); showDropdown = false">
                  <span>个人中心</span>
                </button>
                <button class="dropdown-item" @click="router.push('/upload-video'); showDropdown = false">
                  <span>上传视频</span>
                </button>
                <button class="dropdown-item" @click="router.push('/user/history'); showDropdown = false">
                  <span>观看历史</span>
                </button>
                <button class="dropdown-item" @click="handleLogout(); showDropdown = false">
                  <span>退出登录</span>
                </button>
              </div>
          </div>
        </div>
        <div v-else class="auth-buttons">
          <button class="login-btn" @click="router.push('/login')">登录</button>
          <button class="register-btn" @click="router.push('/register')">注册</button>
        </div>
      </div>
    </div>
  </header>
</template>

<style scoped>
/* 导航栏样式 */
.navbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 60px;
  background-color: rgba(255, 255, 255, 0);
  z-index: 1000;
  transition: var(--transition);
}

.navbar-scrolled {
  background-color: rgba(255, 255, 255, 0.95);
  box-shadow: var(--shadow-sm);
  backdrop-filter: blur(10px);
}

.navbar-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

/* Logo样式 */
.logo {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.logo-icon {
  width: 32px;
  height: 32px;
  margin-right: 8px;
}

.logo-text {
  font-size: 20px;
  font-weight: bold;
  color: var(--primary-color);
}

/* 导航链接样式 */
.nav-links {
  display: flex;
  align-items: center;
  gap: 32px;
}

.nav-link {
  font-size: 16px;
  color: var(--text-primary);
  position: relative;
}

.nav-link:hover {
  color: var(--primary-color);
}

.nav-link::after {
  content: '';
  position: absolute;
  bottom: -4px;
  left: 0;
  width: 0;
  height: 2px;
  background-color: var(--primary-color);
  transition: width 0.3s ease;
}

.nav-link:hover::after {
  width: 100%;
}

/* AI导航链接特殊样式 */
.ai-nav-link {
  position: relative;
  font-weight: 600;
  color: var(--primary-color);
}

.ai-nav-link::after {
  content: '';
  position: absolute;
  bottom: -4px;
  left: 0;
  width: 0;
  height: 2px;
  background-color: var(--primary-color);
  transition: width 0.3s ease;
}

.ai-nav-link:hover::after {
  width: 100%;
}

/* 右侧用户区域 */
.navbar-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

/* 登录注册按钮样式 */
.auth-buttons {
  display: flex;
  gap: 8px;
}

.login-btn,
.register-btn {
  padding: 6px 16px;
  border-radius: var(--border-radius);
  font-size: 14px;
  font-weight: 500;
  transition: var(--transition);
}

.login-btn {
  color: var(--text-primary);
  background-color: transparent;
  border: 1px solid var(--border-color);
}

.login-btn:hover {
  color: var(--primary-color);
  border-color: var(--primary-color);
}

.register-btn {
  color: var(--white);
  background-color: var(--primary-color);
}

.register-btn:hover {
  background-color: var(--primary-color-hover);
}

.nav-button {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  color: var(--text-secondary);
}

.nav-button:hover {
  background-color: #f0f0f0;
  color: var(--primary-color);
}

/* 用户头像容器 */
.user-avatar-container {
  position: relative;
  display: inline-block;
}

/* 用户头像 */
.user-avatar {
  display: flex;
  align-items: center;
  cursor: pointer;
  gap: 8px;
  background: none;
  border: none;
  padding: 0;
}

.user-avatar img {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 2px solid var(--primary-color);
}

.user-name {
  font-size: 14px;
  font-weight: 500;
  max-width: 100px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* 用户下拉菜单 */
.user-dropdown {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 8px;
  background-color: var(--white);
  border-radius: var(--border-radius);
  box-shadow: var(--shadow-md);
  z-index: 1001;
  min-width: 120px;
}

.dropdown-item {
  display: block;
  width: 100%;
  padding: 10px 16px;
  text-align: left;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 14px;
  color: var(--text-primary);
  transition: var(--transition);
}

.dropdown-item:hover {
  background-color: var(--bg-color);
  color: var(--primary-color);
}

.dropdown-item:first-child {
  border-radius: var(--border-radius) var(--border-radius) 0 0;
}

.dropdown-item:last-child {
  border-radius: 0 0 var(--border-radius) var(--border-radius);
}

/* 移动端菜单按钮 */
.mobile-menu-btn {
  display: none;
  width: 40px;
  height: 40px;
  align-items: center;
  justify-content: center;
  color: var(--text-primary);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .mobile-menu-btn {
    display: flex;
  }
  
  .desktop-only {
    display: none;
  }
  
  .user-name {
    display: none;
  }
}
</style>